package cn.itcast.service.impl;

import cn.itcast.dao.cargo.ContractDao;
import cn.itcast.dao.cargo.ContractProductDao;
import cn.itcast.dao.cargo.ExtCproductDao;
import cn.itcast.domain.cargo.*;
import cn.itcast.service.ContractProductService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.UUID;

@Service
public class ContractProductServiceImpl implements ContractProductService {

    @Autowired
    private ContractProductDao contractProductDao;
    @Autowired
    private ContractDao contractDao;
    @Autowired
    private ExtCproductDao extCproductDao;


    @Override
    public PageInfo findAll(ContractProductExample example, int page, int size) {
        PageHelper.startPage(page,size);
        List<ContractProduct> list = contractProductDao.selectByExample(example);
        PageInfo<ContractProduct> pb = new PageInfo<>(list);
        return pb;
    }

    @Override
    public void save(ContractProduct contractProduct) {
        // 1 设置一些货物的额外属性（uuid 总金额...）
        contractProduct.setId(UUID.randomUUID().toString());
        Double total=contractProduct.getPrice()*contractProduct.getCnumber();
        contractProduct.setAmount(total);
        //2 保存货物的数据到数据库
        contractProductDao.insertSelective(contractProduct);
        //3 根据货物的所属合同id查询合同
        Contract contract = contractDao.selectByPrimaryKey(contractProduct.getContractId());
        //4 修改合同的总金额
        contract.setTotalAmount(contract.getTotalAmount()+total);
        //5 修改合同的货物数
        contract.setProNum(contract.getProNum()+1);
        //6 修改合同数据到数据库
        contractDao.updateByPrimaryKeySelective(contract);
    }

    @Override
    public void update(ContractProduct contractProduct) {
        //1 根据货物的id获取到以前的货物对象
        ContractProduct old_contractProduct = contractProductDao.selectByPrimaryKey(contractProduct.getId());
        //2 获取以前的总金额
        Double old_total=old_contractProduct.getAmount();
        //3 计算修改之后的总金额
        Double new_total=contractProduct.getPrice()*contractProduct.getCnumber();
        contractProduct.setAmount(new_total);
        //4 修改货物数据到数据库
        contractProductDao.updateByPrimaryKeySelective(contractProduct);
        //5 根据货物的合同id获取到合同
        Contract contract = contractDao.selectByPrimaryKey(contractProduct.getContractId());
        //6 修改合同的总金额
        contract.setTotalAmount(contract.getTotalAmount()-old_total+new_total);
        //7 修改合同最新数据到数据库
        contractDao.updateByPrimaryKeySelective(contract);


    }

    @Override
    public void delete(String id) {
        // 货物变
            //1 获取货物对象(货物id)
        ContractProduct contractProduct = contractProductDao.selectByPrimaryKey(id);
            //2 货物货物的总金额
        Double total=contractProduct.getAmount();
            //3 删除货物
        contractProductDao.deleteByPrimaryKey(contractProduct.getId());
        // 附件变
            //1 获取附件对象（货物id）
        ExtCproductExample extCproductExample = new ExtCproductExample();
        ExtCproductExample.Criteria criteria = extCproductExample.createCriteria();
        criteria.andContractProductIdEqualTo(id);
        List<ExtCproduct> list = extCproductDao.selectByExample(extCproductExample);
            //2 统计总金额
        for (ExtCproduct extCproduct : list) {
            total+=extCproduct.getAmount();
            //3 删除附件
            extCproductDao.deleteByPrimaryKey(extCproduct.getId());
        }

        // 合同变
            //1 获取合同 （货物的合同id）
        Contract contract = contractDao.selectByPrimaryKey(contractProduct.getContractId());
            //2 修改合同的总金额
        contract.setTotalAmount(contract.getTotalAmount()-total);
            //3 修改合同的货物数
        contract.setProNum(contract.getProNum()-1);
            //4 修改合同的附件数
        contract.setExtNum(contract.getExtNum()-list.size());
            //5 同步合同的最新修改数据
        contractDao.updateByPrimaryKeySelective(contract);

    }

    @Override
    public ContractProduct findById(String id) {

        ContractProduct contractProduct = contractProductDao.selectByPrimaryKey(id);
        return contractProduct;
    }


}
